package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.*;

import models.Location;
import models.SolutionAdoption;
import models.SolutionSubmission;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

@SuppressWarnings("serial")
public class ajxExportReportFinBenefitSummaryServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		resp.setContentType("application/vnd.ms-excel"); 		 
	    resp.setHeader("Content-Disposition", "attachment; filename=ExportFinancialSummary.xls"); 
		PrintWriter out = resp.getWriter();
		String sYear = (String) req.getParameter("year");
		String sMonth = (String) req.getParameter("month");
	

		//out.println("<TABLE><TBODY><TR><TD width=100><SPAN class='lotusBtn lotusBtnAction lotusLeft'><A href='http://opscentral.asia.ad.celestica.com/global/collaboration/SavvySol.nsf/ExportFBSummary?openagent'>Export XL</A></SPAN></TD></TR></TBODY></TABLE>");		
		out.println("<TABLE><TBODY><TR><TD colSpan=12><B>Financial Benefit Summary</B></TD></TR>");
		
		out.println("<TR>");
		out.println("<TD bgColor=#a0a0a0 width=50></TD>");
		out.println("<TD bgColor=#FF9900 class=style1 colSpan=5 align = center><font color=#FFFFFF><B>SHARING</B></font></TD>");
		out.println("<TD bgColor=#008080 class=style2 colSpan=5 align = center><B>ADOPTION</B></TD>");
		out.println("<TD bgColor=#a0a0a0 width=50></TD>");
		out.println("</TR>");
		
		
		out.println("<TR>");
		out.println("<TD class=style0 width=100 bgColor=#C0C0C0><B>Site</B></TD>");
		out.println("<TD class=style3 width=100 bgColor=#FFCC99># Solutions Shared</TD>");
		out.println("<TD class=style3 width=100 bgColor=#FFCC99>Total Savings ($USD)</TD>");
		out.println("<TD class=style3 width=100 bgColor=#FFCC99>Hard P&amp;L Savings ($USD)</TD>");
		out.println("<TD class=style3 width=100 bgColor=#FFCC99>Cost Avoidance ($USD)</TD>");
		out.println("<TD class=style3 width=100 bgColor=#FFCC99>Oppotunity Cost ($USD)</TD>");
		out.println("<TD class=style4 width=100 bgColor=#CCFFCC># Solutions Adopted</TD>");
		out.println("<TD class=style4 width=100 bgColor=#CCFFCC>Total Savings ($USD)</TD>");
		out.println("<TD class=style4 width=100 bgColor=#CCFFCC>Hard P&amp;L Savings ($USD)</TD>");
		out.println("<TD class=style4 width=100 bgColor=#CCFFCC>Cost Avoidance ($USD)</TD>");
		out.println("<TD class=style4 width=100 bgColor=#CCFFCC>Oppotunity Cost ($USD)</TD>");
		out.println("<TD class=style0 width=100 bgColor=#C0C0C0># Solutions Adopted - ratio</TD></TR>");		
		
		
		float totalSavings = 0;
		float hardSavings = 0;
		float costSavings = 0;
		float oppSavings = 0;
		float atotalSavings = 0;
		float ahardSavings = 0;
		float acostSavings = 0;
		float aoppSavings = 0;
		
		int totalAdopt = 0;
		
		Objectify ofy = ObjectifyService.begin();
		
		
		
   		Query<Location> q = ofy.query(Location.class).order("LocationName");   		
   		for ( Location f : q.fetch() ) {
   			out.println("<TR><TD class=style6 width=100>" + f.getLocationName() + "</TD>");
   		
   			Query<SolutionSubmission> qq = null;
   			
   			if ( sYear!= null && sMonth != null) {
   				qq = ofy.query(SolutionSubmission.class).filter("Location", f.getLocationName()).filter("sYear", sYear).filter("sMonth", sMonth);
   			}else if ( sYear!= null ) {
   				qq = ofy.query(SolutionSubmission.class).filter("Location", f.getLocationName()).filter("sYear", sYear);
   			}else if ( sMonth!= null ){
   				qq = ofy.query(SolutionSubmission.class).filter("Location", f.getLocationName()).filter("sMonth", sMonth);
   			}else{
   				qq = ofy.query(SolutionSubmission.class).filter("Location", f.getLocationName());
   			}
   				
   			
   			out.println("<TD class=style6 width=50>"+ qq.countAll() +"</TD>");
   			
   			
   			for ( SolutionSubmission ff : qq.fetch() ) {
   				totalSavings = totalSavings + (Float) ff.getAnnProdSavings();
   				hardSavings = hardSavings + (Float) ff.getHardSavings();
   				costSavings = costSavings + (Float) ff.getCostAvoidance();
   				oppSavings = oppSavings + (Float) ff.getOppurtunityCost();
   			}
   			
   			out.println("<TD class=style6 width=50 >"+ totalSavings +"</TD>");
   			out.println("<TD class=style6 width=50 >"+ hardSavings +"</TD>");
   			out.println("<TD class=style6 width=50 >"+ costSavings +"</TD>");
   			out.println("<TD class=style6 width=50 >"+ oppSavings +"</TD>");
   			
   			totalAdopt = 0;
			Query<SolutionAdoption> qqq = null;
   			if ( sYear!= null && sMonth != null) {
   				qqq = ofy.query(SolutionAdoption.class).filter("Location", f.getLocationName()).filter("sYear", sYear).filter("sMonth", sMonth);
   			}else if ( sYear!= null ) {
   				qqq = ofy.query(SolutionAdoption.class).filter("Location", f.getLocationName()).filter("sYear", sYear);
   			}else if ( sMonth!= null ){
   				qqq = ofy.query(SolutionAdoption.class).filter("Location", f.getLocationName()).filter("sMonth", sMonth);
   			}else{
   				qqq = ofy.query(SolutionAdoption.class).filter("Location", f.getLocationName());
   			}
   			
			totalAdopt = totalAdopt + qqq.countAll();
			for ( SolutionAdoption fff : qqq.fetch() ) {
   				atotalSavings = atotalSavings + (Float) fff.getAnnProdSavings();
   				ahardSavings = ahardSavings + (Float) fff.getHardSavings();
   				acostSavings = acostSavings + (Float) fff.getCostAvoidance();
   				aoppSavings = aoppSavings + (Float) fff.getOppurtunityCost();   					
			}
   			
   			out.println("<TD class=style6 width=50 >"+ totalAdopt +"</TD>");
   			out.println("<TD class=style6 width=50 >"+ atotalSavings +"</TD>");
   			out.println("<TD class=style6 width=50 >"+ ahardSavings +"</TD>");
   			out.println("<TD class=style6 width=50 >"+ acostSavings +"</TD>");
   			out.println("<TD class=style6 width=50 >"+ aoppSavings +"</TD>");
   			
   			totalSavings = hardSavings = costSavings = oppSavings = 0;
   			atotalSavings = ahardSavings = acostSavings = aoppSavings = 0;
   			
   			//DONT KNOW HOW TO CALCULATEif ( totalAdopt > 0 && qq.countAll() > 0 ){
   				//out.println("<TD class=style6 width=50 align=middle>" + qqq.countAll() / qq.countAll() + "%</TD></TR>");
   			//}else{
   				out.println("<TD class=style6 width=50 align=middle>0.0%</TD></TR>");
   			//}
   		}
   		out.println("</TBODY></TABLE>");		
	}
}
